package com.bkgy.modular.alarm.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.alarm.dao.AlarmDao;
import com.bkgy.modular.alarm.entity.ZoneEntity;
import com.bkgy.modular.tbdsjwarninginformation.dao.TbDsjWarningInformationDao;
import com.bkgy.util.CalUtils;
import com.bkgy.util.SDKUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AlarmService {

    @Resource
    private SDKUtils sdkUtils;
    @Resource
    private TbDsjWarningInformationDao tbDsjWarningInformationDao;
    @Resource
    private CalUtils calUtils;
    @Resource
    private AlarmDao alarmDao;
    @Resource
    private RestTemplate restTemplate;

//----    @Scheduled(cron = "${cronTime1}")//定时在112全打开，113，115全部关闭"C621", 4.0查不到数据先注销
    public void alarmA() {
        //log.error("AlarmService+alarmA");
//        try {
//            HDDataConnection connect = sdkUtils.getConnect();
//            String values = sdkUtils.findDataOneById(sdkUtils.findTagId("C621_WCMRH", connect.getTagManager()), connect.getDataProvider());
//            //log.error("AlarmService+alarmA+values:"+values);
//            if (!StringUtils.isEmpty(values)) {
//                double vs = Double.parseDouble(values);
//                if (vs > 5) {
//                    List<Alarm> c621 = alarmDao.findAll("C621", 4.0);
//                    //log.error("AlarmService+alarmA+c621.size():"+c621.size());
//                    c621.forEach(t -> {
//                        if (t.getVariable_property() == 1.0) {
//                            List<Alarm> list = alarmDao.findAll1(t.getL1_variable_alias());
//                            Integer tagId = sdkUtils.findTagId(t.getL1_variable_name(), connect.getTagManager());
//                            String value = sdkUtils.findDataOneById(tagId, connect.getDataProvider());
//
//                            Map<String, List<Alarm>> map = list.stream().collect(Collectors.groupingBy(Alarm::getRule_editor));
//
//                            map.forEach((k, v) -> {
//                                if (k.equals(">")) {
//                                    List<Alarm> list1 = v.stream().sorted(Comparator.comparing(Alarm::getThreshold_value, Comparator.reverseOrder())).collect(Collectors.toList());
//
//                                    for (Alarm z : list1) {
//                                        if (Double.valueOf(value) > z.getThreshold_value()) {
//                                            TbDsjWarningInformation information = new TbDsjWarningInformation();
//                                            information.setWarning_cacl_result(z.getWarning_cacl_result());
//                                            information.setL1_variable_name(t.getL1_variable_name());
//                                            information.setL1_variable_description(t.getL1_variable_description());
//                                            information.setL1_variable_value(Double.valueOf(value));
//                                            information.setWarning_grade(z.getWarning_grade());
//                                            information.setWarning_content(z.getWarning_content());
//                                            information.setWarning_suggestion(z.getWarning_suggestion());
//                                            information.setProcess_rule_variable(z.getProcess_rule_variable());
//                                            information.setThreshold_value(z.getThreshold_value());
//                                            information.setMonitoring_cycle(z.getMonitoring_cycle());
//                                            information.setArea_code(t.getArea_code());
//                                            information.setDevice_code(t.getDevice_code());
////                                information.setShift();
////                                information.setSteel_grade();
//                                            information.setCreate_time(new Timestamp(new Date().getTime()));
//                                            String coilId = findCoilId(t.getDevice_code());
//                                            information.setCoil_id(coilId);
//                                            tbDsjWarningInformationDao.insert(information);
//                                            return;
//                                        }
//                                    }
//                                }
//
//                                if (k.equals("<")) {
//                                    List<Alarm> list2 = v.stream().sorted(Comparator.comparing(Alarm::getThreshold_value)).collect(Collectors.toList());
//                                    for (Alarm z : list2) {
//                                        if (Double.valueOf(value) < z.getThreshold_value()) {
//                                            TbDsjWarningInformation information = new TbDsjWarningInformation();
//                                            information.setWarning_cacl_result(z.getWarning_cacl_result());
//                                            information.setL1_variable_name(t.getL1_variable_name());
//                                            information.setL1_variable_description(t.getL1_variable_description());
//                                            information.setL1_variable_value(Double.valueOf(value));
//                                            information.setWarning_grade(z.getWarning_grade());
//                                            information.setWarning_content(z.getWarning_content());
//                                            information.setWarning_suggestion(z.getWarning_suggestion());
//                                            information.setProcess_rule_variable(z.getProcess_rule_variable());
//                                            information.setThreshold_value(z.getThreshold_value());
//                                            information.setMonitoring_cycle(z.getMonitoring_cycle());
//                                            information.setArea_code(t.getArea_code());
//                                            information.setDevice_code(t.getDevice_code());
////                                information.setShift();
////                                information.setSteel_grade();
//                                            information.setCreate_time(new Timestamp(new Date().getTime()));
//                                            String coilId = findCoilId(t.getDevice_code());
//                                            information.setCoil_id(coilId);
//                                            tbDsjWarningInformationDao.insert(information);
//                                            return;
//                                        }
//                                    }
//                                }
//                            });
//                        }
//
//                        if (t.getVariable_property() == 2.0) {
//                            Integer tagId = sdkUtils.findTagId(t.getL1_variable_name(), connect.getTagManager());
//                            List<String> data = sdkUtils.findDataOneByTime(tagId, connect.getDataProvider(), "30");
//                            List<Double> collect = data.stream().map(Double::parseDouble).collect(Collectors.toList());
//                            Double avg = calUtils.calAvg(collect);
//
//                            if (!StringUtils.isEmpty(avg) && !StringUtils.isEmpty(t.getThreshold_value()) && avg > t.getThreshold_value()) {
//                                TbDsjWarningInformation information = new TbDsjWarningInformation();
//                                information.setWarning_cacl_result(t.getWarning_cacl_result());
//                                information.setL1_variable_name(t.getL1_variable_name());
//                                information.setL1_variable_description(t.getL1_variable_description());
//                                information.setL1_variable_value(Double.valueOf(t.getThreshold_value()));
//                                information.setWarning_grade(t.getWarning_grade());
//                                information.setWarning_content(t.getWarning_content());
//                                information.setWarning_suggestion(t.getWarning_suggestion());
//                                information.setProcess_rule_variable(t.getProcess_rule_variable());
//                                information.setThreshold_value(t.getThreshold_value());
//                                information.setMonitoring_cycle(t.getMonitoring_cycle());
//                                information.setArea_code(t.getArea_code());
//                                information.setDevice_code(t.getDevice_code());
////                                information.setShift();
////                                information.setSteel_grade();
//                                information.setCreate_time(new Timestamp(new Date().getTime()));
//                                String coilId = findCoilId(t.getDevice_code());
//                                information.setCoil_id(coilId);
//                                tbDsjWarningInformationDao.insert(information);
//                            }
//                        }
//                    });
//                }
//            }
//            try {
//                connect.dispose();
//                //log.error("AlarmService+alarmA+connect成功关闭");
//            } catch (HDSdkException e) {
//                //log.error("alarmA() connect.dispose()释放失败");
//                //e.printStackTrace();
//            }
//        } catch (Exception e) {
//            //log.error("AlarmService+alarmA:中间有错误");
//            //e.printStackTrace();
//        }
    }


    private String findCoilId(String deviceCode) {
        String coilId = null;
        Map<String, String> map = new HashMap<>();
        map.put("Shear", "zone0");
        map.put("TL", "zone12");
        map.put("PL1", "zone13");
        map.put("PL2", "zone14");
        map.put("PL3", "zone15");
        map.put("PL4", "zone16");
        map.put("RINSE4", "zone17");
        map.put("EnS1", "zone30");
        map.put("S1S2", "zone31");
        map.put("S1", "zone31");
        map.put("S2S3", "zone32");
        map.put("S2", "zone32");
        map.put("S3S4", "zone33");
        map.put("S3", "zone33");
        map.put("ExS4", "zone34");
        map.put("S4", "zone34");
        try {
            if (!StringUtils.isEmpty(deviceCode)) {
                String zone = map.get(deviceCode);
                if (!StringUtils.isEmpty(zone)) {
                    String data = restTemplate.postForObject("http://170.0.200.11:8090/restapii?name=C621_zonetrk", null, String.class);
                    JSONObject jsonObject = JSONObject.parseObject(data);
                    JSONArray zone_row = jsonObject.getJSONArray("zone_row");
                    List<ZoneEntity> list = JSONObject.parseArray(zone_row.toString(), ZoneEntity.class);
                    ZoneEntity zoneEntity = list.stream().filter(t -> t.getZONENO().equalsIgnoreCase(zone)).findFirst().orElse(null);
                    if (!ObjectUtils.isEmpty(zoneEntity)) {
                        coilId = zoneEntity.getCoilid();
                    }
                }
            }
        } catch (Exception e) {
            log.error("宝信Zone获取失败");
            e.printStackTrace();
        }
        return coilId;
    }


}
